package com.qfedu.aunt.commons.exception;

import com.qfedu.aunt.commons.observer.Subject;
import com.qfedu.aunt.commons.result.R;
import com.qfedu.aunt.commons.result.ResponseCode;
import com.qfedu.aunt.commons.utils.ExceptionUtils;
import com.qfedu.aunt.commons.utils.ThreadPoolUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;

/**
 * @author xiaobobo
 * @title: GlobleException
 * @projectName aunt_commons_2209
 * @description: TODO
 * @date 2022/10/24  15:36
 */

@ControllerAdvice
@Slf4j
public class GlobleException{

    @Autowired
    private Subject subject;







    /**
     * 处理空指针的地方
     * @param err
     * @return
     */
    @ExceptionHandler(NullPointerException.class)
    @ResponseBody    //转换成JSON格式返回到前端
    public Object handlerNullPointerException(NullPointerException err){
        R error = R.error(ResponseCode.NULL_POINT_ERROR.getCode(), ResponseCode.NULL_POINT_ERROR.getMessage()+":"+err.getMessage());
        log.info("出现了空指针异常:{}",ExceptionUtils.getMessage(err));
        return error;
    }
    @ExceptionHandler(AccessDeniedException.class)
    @ResponseBody    //转换成JSON格式返回到前端
    public Object handlerAccessDeniedException(AccessDeniedException err){
        R error = R.error(
                         ResponseCode.UN_AUTHORITION_EXCEPTION.getCode(),
                         ResponseCode.UN_AUTHORITION_EXCEPTION.getMessage());
        log.info("出现了空指针异常:{}",ExceptionUtils.getMessage(err));
        return error;
    }


    /**
     * 处理SQL异常的地方
     * @param err
     * @return
     */
    @ExceptionHandler(SQLException.class)
    @ResponseBody    //转换成JSON格式返回到前端
    public Object handlerException(SQLException err){
        R error = R.error(ResponseCode.SQL_ERROR.getCode(), ResponseCode.SQL_ERROR.getMessage()+":"+err.getMessage());
        log.info("出现了SQL异常:{}",ExceptionUtils.getMessage(err));
        return error;
    }


    /**
     * 这个是处理业务异常的地方
     * @param err
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    @ResponseBody    //转换成JSON格式返回到前端
    public Object handlerException(BusinessException err){
        R error = R.error(err.getCode(), err.getMessage());
        log.info("出现了业务异常:{}",ExceptionUtils.getMessage(err));
        return error;
    }


    /**
     * 处理系统异常的地方
     * @param err
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody    //转换成JSON格式返回到前端
    public Object handlerException(Exception err){
        R error = R.error(ResponseCode.SYSTEM_ERROR.getCode(), ResponseCode.SYSTEM_ERROR.getMessage()+":"+err.getMessage());
        log.error("出现了系统异常:{}", ExceptionUtils.getMessage(err));
        //在这里要通过我们的线程池来实施短信和邮件的发送
        ThreadPoolUtils.getThreadPoolExecutor().execute(
                new Runnable() {
                    @Override
                    public void run() {
                        subject.notifyOperator(ExceptionUtils.getMessage(err));
                    }
                }
        );
        return error;
    }



}

